package com.example.teamtribe.main.club.clubintroduce.media.photo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.main.club.browse.BrowseActivity;
import com.example.teamtribe.utils.Utility;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/6/16.
 */

public class PhotoGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    public static int PHOTO=1;//内容为图片
    public static int LOCALMEDIA=2;//内容为LocalMedia对象

    private Context context;
    private List<LocalMedia> mediaList;
    private List<String>photoList;
    private int type;

    public PhotoGridAdapter(int type){
        this.type=type;
        photoList = new ArrayList<>();
        mediaList = new ArrayList<>();
    }
    public void addPhoto(String photo) {
        photoList.add(photo);
    }
    public void setList(List<LocalMedia> medias) {
        mediaList=medias;
    }
    public void clear(int type){
        if(mediaList!=null&&type==LOCALMEDIA)
            mediaList.clear();
        else
            photoList.clear();
    }
    static class MediaViewHolder extends RecyclerView.ViewHolder {
        ImageView photo;

        public MediaViewHolder(View itemView) {
            super(itemView);
            photo = itemView.findViewById(R.id.iv_photo);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        context = parent.getContext();
        final RecyclerView.ViewHolder holder;
            final View view = LayoutInflater.from(context).inflate(R.layout.item_club_photo, parent, false);
            holder = new MediaViewHolder(view);
            if(type==PHOTO){
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getAdapterPosition();
                        Intent intent = new Intent(context, BrowseActivity.class);
                        intent.putExtra("images", (ArrayList) photoList);
                        intent.putExtra("position", position);
                        context.startActivity(intent);
                    }
                });
            }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(type==PhotoGridAdapter.LOCALMEDIA) {
            LocalMedia media = mediaList.get(position);
            File file = new File(media.getPath());
            Glide.with(context).load(file).into(((MediaViewHolder) holder).photo);
        }else{
            Glide.with(context).load(Utility.getUri(photoList.get(position))).into(((MediaViewHolder) holder).photo);
        }
    }

    @Override
    public int getItemCount() {
        if(type==PhotoGridAdapter.LOCALMEDIA)
            return mediaList.size();
        else
            return photoList.size();
    }
}
